package com.erp4j.dynamicfield.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.erp4j.dynamicfield.entity.Customer;
import com.erp4j.dynamicfield.service.CustomerService;
import com.erp4j.dynamicfield.vo.QueryParam;
import com.erp4j.dynamicfield.vo.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/customers")
public class CustomerController {

    @Resource
    private CustomerService customerService;

    /**
     * 分页查询客户数据
     */
    @PostMapping("/query/{moduleId}")
    public Result<IPage<Customer>> queryCustomers(
            @PathVariable Long moduleId,
            @RequestBody List<QueryParam> params,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return Result.success(customerService.queryByParams(moduleId, params, pageNum, pageSize));
    }

    /**
     * 根据ID获取客户
     */
    @GetMapping("/{id}")
    public Result<Customer> getCustomer(@PathVariable Long id) {
        return Result.success(customerService.getById(id));
    }

    /**
     * 保存或更新客户
     */
    @PostMapping
    public Result<Customer> saveCustomer(@RequestBody Customer customer) {
        customerService.saveOrUpdateCustomer(customer);
        return Result.success(customer);
    }

    /**
     * 删除客户
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteCustomer(@PathVariable Long id) {
        customerService.removeById(id);
        return Result.success();
    }
}
